const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');

const isProd = process.env.NODE_ENV === 'production';

module.exports = {
  mode: 'development',
  entry: {
    index: './index.js',
  },
  devtool: 'inline-source-map',
  devServer: {
    static: {
      watch: true,
      directory: path.join(__dirname, 'dist'),
    },
    open: true,
    hot: true,
    client: {
      overlay: true,
    },
  },
  module: {
    rules: [
      {
        test: /\.(css|less|sass)$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
      }, // 不用搞file-loader url-loader 用type定义
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024, //小于 8kb 的文件，将会视为 inline 模块类型，否则会被视为 resource 模块类型。
          },
        },
        generator: {
          filename: 'images/[base]',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[base]',
        },
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].[hash:8].css',
      ignoreOrder: false,
    }),
    new HtmlWebpackPlugin({
      title: 'svg',
      template: './index.html',
    }),
    new CleanWebpackPlugin(),
  ],
  optimization: {
    moduleIds: 'named',
  },
  output: {
    filename: '[name].[hash:8].ble.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: isProd ? './' : '/',
  },
};
